/*   ro-npcs, a plugin for the Rolgps, part of the RolGPS Proyect
 *   Copyright (C) 2009 Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */


package ronpcsApp;

import dataTypes.Ciudad;
import dataTypes.DataSource;
import dataTypes.Lugar;
import dataTypes.Npc;
import dataTypes.Profesion;
import dataTypes.Reino;
import java.awt.Toolkit;
import java.io.File;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  xeph
 */
public class RoAbm extends javax.swing.JInternalFrame {
    
    //Directorio de trabajo del plugin
    private String dir="";
    
    //variables que definen el estado de los botones
    private final int SEARCH=1;
    private final int EXISTS=2;
    private final int NOTEXISTS = 3;
    
    //Vector que define la lista
    Vector lista = new Vector();
    
    //Lista con la info de los npcs
    Npc[] npcsLista = new Npc[0];
    
    //Modelo de npc (usado por actualizaciones y otras cosas
    Npc modelo = new Npc();
    
    //Lista de reinos
    Reino[] listaReinos = new Reino[0];
    
    //Lista de ciudades
    Ciudad[] listaCiudades = new Ciudad[0];
    
    //Lista de profesiones
    Profesion[] listaProfesiones = new Profesion[0];
    
    //Archivo de Base de datos
    File databaseFile = new File(this.dir + File.separator + "npcs.sqlite3");
    
    //Modo experto (regula el uso de la abm)
    boolean expertmode=false;
    
    //Modo standalone
    private boolean standalone=false;
    
    //Modo botones
    private boolean legacyButtonMode=false;
    
    /** Creates new form RoAbm */
    public RoAbm(String dir) {
        this.dir=dir;
        initComponents();
        setButtonState(SEARCH);
        verificarBaseDeDatos();
        actualizarListas();
    }
    
    
    public void setStandalone(boolean standalone) {
        this.standalone = standalone;
    }
    
    /** Define el estado de los botones, de manera tal que no tenga que 
     reescribirse cada vez que sea necesario.*/
    private void setButtonState(int state){
        if(!expertmode){
            btnBorrar.setVisible(false);
            btnNuevo.setVisible(false);
            btnModificar.setVisible(false);
            switch(state){
                case SEARCH:
                    btnBuscar.setEnabled(true);
                    btnBorrar.setEnabled(false);
                    btnNuevo.setEnabled(false);
                    btnModificar.setEnabled(false);
                    jListNpcs.setEnabled(false);
                    break;

                case NOTEXISTS:
                    if(legacyButtonMode){
                        btnBuscar.setEnabled(false);
                    }else{
                        btnBuscar.setEnabled(true);
                    }
                    btnBorrar.setEnabled(false);
                    btnNuevo.setEnabled(false);
                    btnModificar.setEnabled(false);
                    jListNpcs.setEnabled(false);
                    break;

                case EXISTS:
                    if(legacyButtonMode){
                        btnBuscar.setEnabled(false);
                    }else{
                        btnBuscar.setEnabled(true);
                    }
                    btnBorrar.setEnabled(false);
                    btnNuevo.setEnabled(false);
                    btnModificar.setEnabled(false);
                    jListNpcs.setEnabled(true);
                    break;                
            }
        }else{
            btnBorrar.setVisible(true);
            btnNuevo.setVisible(true);
            btnModificar.setVisible(true);
            switch(state){
                case SEARCH:
                    btnBuscar.setEnabled(true);
                    btnBorrar.setEnabled(false);
                    btnNuevo.setEnabled(false);
                    btnModificar.setEnabled(false);
                    jListNpcs.setEnabled(false);
                    break;

                case NOTEXISTS:
                    if(legacyButtonMode){
                        btnBuscar.setEnabled(false);
                    }else{
                        btnBuscar.setEnabled(true);
                    }
                    btnBorrar.setEnabled(false);
                    btnNuevo.setEnabled(true);
                    btnModificar.setEnabled(false);
                    jListNpcs.setEnabled(false);
                    break;

                case EXISTS:
                    if(legacyButtonMode){
                        btnBuscar.setEnabled(false);
                    }else{
                        btnBuscar.setEnabled(true);
                    }
                    btnBorrar.setEnabled(true);
                    btnNuevo.setEnabled(false);
                    btnModificar.setEnabled(true);
                    jListNpcs.setEnabled(true);
                    break;                
            }
        }
    }
    
    
    private void actualizarListas(){
        DataSource ds = new DataSource(databaseFile);
        listaCiudades = ds.getCiudades();
        listaProfesiones = ds.getProfesiones();
        listaReinos = ds.getReinos();
        jComboCiudad.removeAllItems();
        for(Ciudad ciudad : listaCiudades){
            jComboCiudad.addItem(ciudad.getName());
        }
        jComboReino.removeAllItems();
        for(Reino reino : listaReinos){
            jComboReino.addItem(reino.getName());
        }
        jComboProfesion.removeAllItems();
        for(Profesion profesion : listaProfesiones){
            jComboProfesion.addItem(profesion.getName());
        }
    }
    
    private void limpiarDatos(boolean borrarLista, boolean borrarModelo){
        lista.clear();
        if(borrarLista) jListNpcs.setListData(lista);
        if(borrarModelo) modelo=new Npc();
        jComboReino.setSelectedIndex(0);
        jComboCiudad.setSelectedIndex(0);
        jComboProfesion.setSelectedIndex(0);
        jTextNombre.setText("");
        jTextX.setText("");
        jTextY.setText("");
        jTextZ.setText("");
    }
    
    private void limpiarCampos(){
        jComboReino.setSelectedIndex(0);
        jComboCiudad.setSelectedIndex(0);
        jComboProfesion.setSelectedIndex(0);
        jTextNombre.setText("");
        jTextX.setText("");
        jTextY.setText("");
        jTextZ.setText("");
    }
    
    //se llama a este método siempre que se inicie la aplicacion.
    private void verificarBaseDeDatos(){

        databaseFile = new File(this.dir+File.separator+"npcs.sqlite3");
        if(!databaseFile.exists()){
            DataSource database = new DataSource(databaseFile);
            database.deploy();
        }else{
            if(!databaseFile.canRead()){
                dispose();
                Toolkit.getDefaultToolkit().beep(); //Beeps.
            }
        }
    }
    
    private void actualizarListaSeleccionable(){
        lista.clear();
        if(npcsLista.length>0){
            for(Npc npc : npcsLista){
                lista.add(npc.getName() + " ("+npc.getReino().getName()+")");
            }
        }
        jListNpcs.setListData(lista);
    }
    
    private Npc obtenerDatosDelUsuario(){
        Npc modeloTemp = new Npc();
        modeloTemp.setName(jTextNombre.getText());    
        Ciudad ciudad = listaCiudades[jComboCiudad.getSelectedIndex()];
        modeloTemp.setCiudad(ciudad);
        Reino reino = listaReinos[jComboReino.getSelectedIndex()];
        modeloTemp.setReino(reino);
        Profesion profesion = listaProfesiones[jComboProfesion.getSelectedIndex()];
        modeloTemp.setProfesion(profesion);
        Lugar coordenadas = new Lugar();
        int x=-1;
        int y=-1;
        int z=-1;
        if(!jTextX.getText().isEmpty()){
            x=Integer.parseInt(jTextX.getText());
        }
        
        if(!jTextY.getText().isEmpty()){
            y=Integer.parseInt(jTextY.getText());
        }
        
        if(!jTextZ.getText().isEmpty()){
            z=Integer.parseInt(jTextZ.getText());
        }
        coordenadas.setX(x);
        coordenadas.setY(y);
        coordenadas.setZ(z);
        modeloTemp.setLugar(coordenadas);
        modeloTemp.setUserId("");
        return modeloTemp;
    }
    private void buscar(){
        buscarModelo(obtenerDatosDelUsuario());
    }
    
    private void buscarModelo(Npc model){
        DataSource database = new DataSource(databaseFile);
        npcsLista = database.buscarNpc(model);
        actualizarListaSeleccionable();
        if(npcsLista.length>0){
            setButtonState(EXISTS);
            limpiarCampos();
        }else{
            setButtonState(NOTEXISTS);
        }
    }
    
    private void nuevoNpc(){
        
        //se debe verificar que los campos no esten vacios
        //como solo se puede usar esta opcion en modo experto
        //no me preocupa mucho verificarlo.
        
        modelo=obtenerDatosDelUsuario();
        modelo.setUserId("2");
        
        DataSource ds= new DataSource(databaseFile);
        ds.agregarNpc(modelo);
        limpiarCampos();
        buscarModelo(modelo);
    }
    
    private void borrarNpc(){
        
    }
    
    private void modificarNpc(){
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListNpcs = new javax.swing.JList();
        jTextNombre = new javax.swing.JTextField();
        jTextX = new javax.swing.JTextField();
        jTextY = new javax.swing.JTextField();
        jTextZ = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jComboCiudad = new javax.swing.JComboBox();
        jComboProfesion = new javax.swing.JComboBox();
        jComboReino = new javax.swing.JComboBox();

        setBorder(null);
        setTitle("NPC Search Engine - RolGPS Proyect, 2009 - Modo Usuario");

        jLabel1.setText("Nombre");

        jLabel2.setText("Ciudad");

        jLabel3.setText("Profesión");

        jLabel4.setText("Reino");

        jLabel5.setText("Coordenadas:");

        jLabel6.setText("X:");

        jLabel7.setText("Y:");

        jLabel8.setText("Z:");

        jListNpcs.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListNpcs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListNpcsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListNpcs);

        jTextX.setColumns(6);

        jTextY.setColumns(6);

        jTextZ.setColumns(6);

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnBorrar.setText("Borrar");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboReino, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboProfesion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboCiudad, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnLimpiar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnBorrar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnModificar, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jComboCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jComboProfesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jComboReino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jTextX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jTextY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addComponent(jTextZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnNuevo)
                            .addComponent(btnBuscar)
                            .addComponent(btnBorrar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLimpiar)
                            .addComponent(btnModificar)
                            .addComponent(btnSalir))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        int index = jListNpcs.getSelectedIndex();
        if(index>=0 && index<npcsLista.length){
            DataSource ds = new DataSource(databaseFile);
            //System.err.println("id: "+modelo.getId()+", nombre: " + modelo.getName());
            Npc aBorrar = new Npc();
            aBorrar.setId(npcsLista[index].getId());
            ds.borrarNpc(aBorrar);
            //System.out.println(aBorrar.getId());
            limpiarCampos();
            buscarModelo(modelo);
        }else{
            System.err.println("Index: "+ index + ", npcsLista: "+npcsLista.length);
        }
}//GEN-LAST:event_btnBorrarActionPerformed


    private void jListNpcsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListNpcsMouseClicked
        //aqui es donde ocurre la magia (?)
        //se setean los campos segun el npc seleccionado de la lista
        //de resultados.
        int index = jListNpcs.getSelectedIndex();
        if(index>=0){
            modelo=npcsLista[index];
            jTextNombre.setText(npcsLista[index].getName());
            jTextX.setText(String.valueOf(npcsLista[index].getLugar().getX()));
            jTextY.setText(String.valueOf(npcsLista[index].getLugar().getY()));
            jTextZ.setText(String.valueOf(npcsLista[index].getLugar().getZ()));

            int iterator=0;
            for(Ciudad ciudad : listaCiudades){
                if(ciudad.getId().equals(npcsLista[index].getCiudad().getId())){
                    jComboCiudad.setSelectedIndex(iterator);
                }
                iterator++;
            }
            
            iterator=0;
            for(Reino reino : listaReinos){
                if(reino.getId().equals(npcsLista[index].getReino().getId())){
                    jComboReino.setSelectedIndex(iterator);
                }
                iterator++;
            }
            
            iterator=0;
            for(Profesion profesion : listaProfesiones){
                if(profesion.getId().equals(npcsLista[index].getProfesion().getId())){
                    jComboProfesion.setSelectedIndex(iterator);
                }
                iterator++;
            }
            
            
        }
    }//GEN-LAST:event_jListNpcsMouseClicked



    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        //hablilitar codigos de modificacion
        if(jTextNombre.getText().startsWith("-set")){
            String argumentos[] = jTextNombre.getText().split(" ");
            if(argumentos.length>1){
                if(argumentos[1].equals("expert") && argumentos[2].equals("on")){
                    expertmode=true;
                    limpiarCampos();
                    this.setTitle("MODO EXPERTO- NPC Search Engine - RolGPS Proyect, 2009");        
                    setButtonState(SEARCH);
                }else if(argumentos[1].equals("expert") && argumentos[2].equals("off")){
                    expertmode=false;
                    setButtonState(SEARCH);
                    this.setTitle("NPC Search Engine - RolGPS Proyect, 2009 - Modo Usuario");
                    limpiarCampos();
                }else if(argumentos[1].equals("buttons") && argumentos[2].equals("legacy")){
                    legacyButtonMode=true;
                    limpiarCampos();
                    setButtonState(SEARCH);
                }else if(argumentos[1].equals("buttons") && argumentos[2].equals("new")){
                    legacyButtonMode=false;
                    setButtonState(SEARCH);
                    limpiarCampos();
                }
            }
        }else{
            buscar();
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
        nuevoNpc();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        // TODO add your handling code here:
        limpiarDatos(true, true);
        setButtonState(SEARCH);
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            DataSource ds = new DataSource(databaseFile);     
            Npc modelotemp = obtenerDatosDelUsuario();
            modelotemp.setId(modelo.getId());
            ds.modificarNpc(modelotemp);
            limpiarCampos();
            buscarModelo(modelo);
            setButtonState(SEARCH);
        } catch (Exception ex) {
            Logger.getLogger(RoAbm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        if(standalone){
            System.exit(0);
        }else{
            super.dispose();
        }
    }//GEN-LAST:event_btnSalirActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox jComboCiudad;
    private javax.swing.JComboBox jComboProfesion;
    private javax.swing.JComboBox jComboReino;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jListNpcs;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextNombre;
    private javax.swing.JTextField jTextX;
    private javax.swing.JTextField jTextY;
    private javax.swing.JTextField jTextZ;
    // End of variables declaration//GEN-END:variables

    
}
